<?php
/**
 * Created by PhpStorm.
 * User: zhuyujun
 * Date: 2018/1/29
 * Time: 16:10
 */

namespace App\Http\Admin\Examine\Controllers;

//调用系统类
use Framework\BaseClass\Http\Admin\Controller;
use Illuminate\Http\Request;

use App\Eloquent\Ygt\PurchasingExamineProcess;
use App\Eloquent\Ygt\DepartmentUser;

class IndexController extends Controller
{
    //采购审批流程
    public function getPurchasingExamineProcess()
    {
        return $this->view('purchasing-examine-process-list');
    }

    //采购审批流程-数据获取
    public function getPurchasingExamineProcessData()
    {
        //后台获取用户对应的企业id
        $adminUser = request()->user('admin')->toArray();
        $companyId = $adminUser['company_id'];

        $callBack = request('callback');

        $purchasingExamineProcessList = PurchasingExamineProcess::where(['company_id' => $companyId])->get();

        foreach ($purchasingExamineProcessList as $key => $purchasingExamineProcessRow){
            $userInfo         = DepartmentUser::getInfoByCompanyIdAndUserId($companyId,$purchasingExamineProcessRow['uid']);
            $purchasingExamineProcessList[$key]['user_name'] =$userInfo['truename'];
        }

        echo $callBack . '(' . $purchasingExamineProcessList->toJson() . ')';
        die();
    }

    //编辑采购审批流程
    public function editPurchasingExamineProcess()
    {
        $processId = request('process_id');
        $processRow = PurchasingExamineProcess::where(['id' => $processId])->first();

        //后台获取用户对应的企业id
        $adminUser = request()->user('admin')->toArray();
        $companyId = $adminUser['company_id'];

        if (empty($processRow)) {
            //定义个空数组，防止报错
            $processRow = [
                'id' => 0,
                'title' => ''
            ];
        }

        //获取分配用户列表
        $where = ['company_id' => $companyId];
        $userList = DepartmentUser::getList($where);
        $selectUserList = [];
        foreach ($userList as $userRow){
            $selectUserList[] = [
                'uid'   => $userRow['user_id'],
                'truename'   => $userRow['truename']
            ];
        }
        return $this->view('purchasing-examine-process-edit', compact('processRow','selectUserList'));
    }

    //编辑采购审批流程-提交
    public function editPurchasingExamineProcessSubmit()
    {
        $processId = request('process_id');
        $title = request('title');
        $uid = request('uid');

        //后台获取用户对应的企业id
        $adminUser = request()->user('admin')->toArray();
        $companyId = $adminUser['company_id'];

        $purchasingExamineProcessObj = PurchasingExamineProcess::firstOrNew(['id' => $processId]);
        $processRow = [
            'title' => $title,
            'uid' => $uid,
            'company_id' => $companyId,
            'step' => 0,
        ];
        $purchasingExamineProcessObj->fill($processRow);
        $purchasingExamineProcessObj->save();

        return [
            'code' => 0,
            'message' => '操作成功'
        ];
    }

}